﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Security.Cryptography;

public class RegFunctions
{
    #region Variables
    RegistryKey rk = Registry.LocalMachine;
    Random Rand = new Random();
    HashAlgorithm Alg = new MD5CryptoServiceProvider();
    string Regcode = "";
    string RegName = "";
    #endregion
    private string hashstr(string src)
    {
        string resu = "";
        byte[] bytesrc = Encoding.UTF8.GetBytes(src);
        byte[] byteres = Alg.ComputeHash(bytesrc);
        resu = Convert.ToBase64String(byteres);
        resu = resu.Substring(0, resu.Length - 2);
        return resu;
    }//هش کردن ام دی فایو یک رشته
    public bool CheckReg()
    {
        bool boolresult = false;
        try
        {
            
            //rk = Registry.CurrentUser;
            //rk = rk.CreateSubKey(@"SOFTWARE\Placement");
            //if (rk.GetValue("RegCode", "").ToString() == "")
            //{
            //    Regcode = Convert.ToString(Rand.Next(1064521, 9976542));
            //    rk.SetValue("RegCode", hashstr(Regcode), RegistryValueKind.String);
            //}
            //Regcode = rk.GetValue("RegCode", "").ToString();
            //RegName = hashstr(Regcode + Environment.MachineName);
            //string key = hashstr("REG" + RegName);
            //if (rk.GetValue("RegKey", "").ToString() == key)
            //{
            //    boolresult = true;
            //}
            plvs.Reg.settingReg.Default.Name = Convert.ToString(hashstr("Reging" + Environment.MachineName));
            plvs.Reg.settingReg.Default.Save();
            if (plvs.Reg.settingReg.Default.Key == hashstr("Keying" + plvs.Reg.settingReg.Default.Name))
            {
                boolresult = true;
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
        return boolresult;
    }// چک کردن ثبت شدن برنامه
    public string getRegName()
    {
        //CheckReg();
        //Regcode = rk.GetValue("RegCode", "").ToString();
        //return hashstr(Regcode + Environment.MachineName);
        return plvs.Reg.settingReg.Default.Name;
    }//ایجاد کد ثبتی که برنامه به کاربر می دهد
    public void setregkey(string src)
    {
        //CheckReg();
        //Regcode = rk.GetValue("RegCode", "").ToString();
        //string key = hashstr("REG" + hashstr(Regcode + Environment.MachineName));
        if (src == hashstr("Keying" + plvs.Reg.settingReg.Default.Name))
        {
            //rk = Registry.LocalMachine;
            //rk = rk.CreateSubKey("SOFTWARE\\Placement");
            //rk.SetValue("RegKey", src, RegistryValueKind.String);
            plvs.Reg.settingReg.Default.Key = src;
            plvs.Reg.settingReg.Default.Save();
            System.Windows.Forms.MessageBox.Show("کلید برنامه ثبت شد", "Info. . .", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("کلید برنامه صحیح نیست", "Info. . .", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
        }
    }//نوشتن کد ثبت وارد شده
}